package io.github.enbool.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.stereotype.Component;

import io.github.enbool.exception.CheckException;



/**
 * 校验工具类
 * 
 * @author 肖文杰 https://xwjie.github.io/PLMCodeTemplate/
 *
 */
@Component
public class CheckUtil {
	/*private static MessageSource resources;
	
	@Autowired
	private MessageSource rs;

	@PostConstruct
	public void setResources() {
		CheckUtil.resources = rs;
	}*/

	public static void check(boolean condition, String msgKey) {
		if (!condition) {
			fail(msgKey);
		}
	}

	public static void notEmpty(String str, String msgKey) {
		if (str == null || str.isEmpty()) {
			fail(msgKey);
		}
	}

	public static void notNull(Object obj, String msgKey) {
		if (obj == null) {
			fail(msgKey);
		}
	}

	/*private static void fail(String msgKey, Object... args) {
		throw new CheckException(resources.getMessage(msgKey, args, Locale.SIMPLIFIED_CHINESE));
	}*/
	
	private static void fail(String message) {
		throw new CheckException(message);
	}
	
	public static void isPhone(String phone) {
	    String regex = "1[1-9][0-9]\\d{8}";
	    if (phone.length() != 11) {
	       fail("手机号应为11位数");
	    }
	    
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(phone);
        boolean isMatch = m.matches();
        
        if (!isMatch) {
        	fail("请填入正确的手机号");
        }	    
	}
}